function varargout = laser(varargin)
% LASER M-file for laser.fig
%      LASER, by itself, creates a new LASER or raises the existing
%      singleton*.
%
%      H = LASER returns the handle to a new LASER or the handle to
%      the existing singleton*.
%
%      LASER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LASER.M with the given input arguments.
%
%      LASER('Property','Value',...) creates a new LASER or raises
%      the existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before laser_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to laser_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help laser

% Last Modified by GUIDE v2.5 10-Jun-2011 13:17:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @laser_OpeningFcn, ...
                   'gui_OutputFcn',  @laser_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before laser is made visible.
function laser_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to laser (see VARARGIN)

% Choose default command line output for laser
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

initialize_gui(hObject, handles, false);

% UIWAIT makes laser wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = laser_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes during object creation, after setting all properties.
function dt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function dt_Callback(hObject, eventdata, handles)
% hObject    handle to dt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of dt as text
%        str2double(get(hObject,'String')) returns contents of dt as a double
dt = str2double(get(hObject, 'String'));
if isnan(dt)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end

% Save the new dt value
handles.metricdata.dt = dt;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function LASERTIME_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LASERTIME (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function LASERTIME_Callback(hObject, eventdata, handles)
% hObject    handle to LASERTIME (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of LASERTIME as text
%        str2double(get(hObject,'String')) returns contents of LASERTIME as a double
LASERTIME = str2double(get(hObject, 'String'));
if isnan(LASERTIME)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end

% Save the new LASERTIME value
handles.metricdata.LASERTIME = LASERTIME;
guidata(hObject,handles)

% --------------------------------------------------------------------
function initialize_gui(fig_handle, handles, isreset)
% If the metricdata field is present and the stop flag is false, it means
% we are we are just re-initializing a GUI by calling it from the cmd line
% while it is up. So, bail out as we dont want to stop the data.
if isfield(handles, 'metricdata') && ~isreset
    return;
end

handles.metricdata.dt = 10000;
handles.metricdata.LASERTIME  = 175000;
handles.metricdata.laserdelay  = 0;

set(handles.dt, 'String', handles.metricdata.dt);
set(handles.LASERTIME,  'String', handles.metricdata.LASERTIME);
set(handles.laserdelay, 'String', handles.metricdata.laserdelay );

% Update handles structure
guidata(handles.figure1, handles);


function laserdelay_Callback(hObject, eventdata, handles)
% hObject    handle to laserdelay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of laserdelay as text
%        str2double(get(hObject,'String')) returns contents of laserdelay as a double
laserdelay = str2double(get(hObject, 'String'));
if isnan(laserdelay)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.metricdata.laserdelay = laserdelay;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function laserdelay_CreateFcn(hObject, eventdata, handles)
% hObject    handle to laserdelay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
DG645LaserCtrl(handles.metricdata.dt,handles.metricdata.laserdelay,handles.metricdata.LASERTIME)


% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject    handle to stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
DG645Stop();